package com.sinoglobal.dumping.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.base.Dumpling_ShareAbstractActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoBaseActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoConfig;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.base.Dumpling_SinoValueManager;
import com.sinoglobal.dumping.bean.Dumpling_ShareInfoResultList;
import com.sinoglobal.dumping.util.Dumpling_HttpMethodSet;
import com.sinoglobal.dumping.util.DumplingUtils;
import com.sinoglobal.wallet.activity.W_HomeActivity;

/**
 * Created by lxy on 2015/11/5.
 */
import static com.sinoglobal.dumping.util.DumplingUtils.*;
import static com.sinoglobal.dumping.base.Dumpling_SinoConstans.*;
public class Dumpling_ReceiveMoneyActivity extends Dumpling_ShareAbstractActivity {
    private ImageView imgIcon;                                  // 图标
    private TextView tvPromptTitle;                             // 提示标题
    private TextView tvLeft;                                    // 左按钮
    private TextView tvRight;                                   // 右按钮
    private TextView tvWallet;                                  // 去钱包按钮
    private TextView tvNumber;                                  // 次数的显示
    private String shareNum;                                    // 剩余次数  
    private Dumpling_ShareInfoResultList resultList;            // 分享实体类
    private Context mContext;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.dumpling_activity_receive);
        shareNum = String.valueOf(DumplingUtils.getCount(this));
        switch (getType(this)){
            case TYPE_DUMPLING_MONEY:
                resultList = Dumpling_SinoConfig.sMoneyShare.getResultList();
                Dumpling_SinoConfig.sMoneyShare = null;
                break;
            case TYPE_DUMPLING_COUPON_3:
            case TYPE_DUMPLING_COUPON_5:
            case TYPE_DUMPLING_COUPON_6:
            case TYPE_DUMPLING_COUPON_7:
                resultList = Dumpling_SinoConfig.sCouponShare.getResultList();
                Dumpling_SinoConfig.sCouponShare = null;
        }
        mContext = Dumpling_ReceiveMoneyActivity.this;
        initView();
        initClick();
        initData();
    }

    private void initView() {
        imgIcon = getView(R.id.activity_receive_icon);
        tvPromptTitle = getView(R.id.activity_receive_prompt_title);
        tvLeft = getView(R.id.activity_receive_left);
        tvRight = getView(R.id.activity_receive_right);
        tvWallet = getView(R.id.activity_receive_wallet);
        tvNumber = getView(R.id.activity_receive_number);
    }

    private void initClick() {
        tvLeft.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        tvWallet.setOnClickListener(this);
    }

    private void initData() {
        tvNumber.setText(!TextUtils.isEmpty(shareNum) && !"0".equals(shareNum) ? numberManager(shareNum) : "");
        tvNumber.setVisibility(!TextUtils.isEmpty(shareNum) ? View.VISIBLE : View.GONE);
        tvLeft.setVisibility(!TextUtils.isEmpty(shareNum) && !"0".equals(shareNum) ? View.VISIBLE : View.GONE);
    }

    private void goWallet() {
        try {
            Intent intent = new Intent();
            intent.setClass(Dumpling_ReceiveMoneyActivity.this, W_HomeActivity.class);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void goLogin() {
        goIntent(Dumpling_NoPasswordQuicklyLoginActivity.class);
    }

    private String numberManager(String num) {
        String number;
        if ("1".equals(num)) {
            number = "一";
        } else if ("2".equals(num)) {
            number = "二";
        } else if ("3".equals(num)) {
            number = "三";
        } else if ("4".equals(num)) {
            number = "四";
        } else if ("5".equals(num)) {
            number = "五";
        } else if ("6".equals(num)) {
            number = "六";
        } else if ("7".equals(num)) {
            number = "七";
        } else if ("8".equals(num)) {
            number = "八";
        } else if ("9".equals(num)) {
            number = "九";
        } else if ("10".equals(num)) {
            number = "十";
        } else if ("11".equals(num)) {
            number = "十一";
        } else if ("12".equals(num)) {
            number = "十二";
        } else {
            number = "多";
        }
//        return "还有" + number + "次机会呦~";
        return "马上分享，再得一次机会~";
    }

    @Override
    public void onAntiViolence(View v) {
        int id = v.getId();
        if (id == R.id.activity_receive_left) {             // 去分享
            if (!Dumpling_SinoBaseActivity.isNoLogIn(mContext)) {
                // 分享
                if (resultList != null) {
                    setShare(resultList.getContent(),
                            resultList.getTitle(), null, resultList.getUrl() + "?productCode=" + Dumpling_SinoConstans.PRODUCT_CODE, 0, resultList.getPicurl());
                    // 分享的同时请求后台
                    Dumpling_HttpMethodSet.markShare(mContext, Dumpling_SinoConstans.USER_ID);
                    int markShareCount =Integer.parseInt((String) Dumpling_SinoValueManager.getValue(Dumpling_ReceiveMoneyActivity.this, Dumpling_SinoConstans.DUMPLING_MARK_SHARE, 0));
                    markShareCount++;
                    Dumpling_SinoValueManager.putValue(Dumpling_ReceiveMoneyActivity.this, Dumpling_SinoConstans.DUMPLING_MARK_SHARE, markShareCount, true);
                }
            } else {
                showToast("正在跳转登录页面");
                goLogin();
            }
        } else if (id == R.id.activity_receive_right) {     // 继续捞
            finish();
        } else if (id == R.id.activity_receive_wallet) {    // 去钱包
            if (isNoLogIn(mContext)) {
                goLogin();
            } else {
                goWallet();
            }
        }
        
    }
    
}
